/**
 * Created by tedi on 2018/3/20.
 */
odoo.define('lift_base.odoo_widget_extend', function (require) {
"use strict";

    var core = require('web.core');
    var session = require('web.session');
    var form_common = require('web.form_common');
    var formats = require('web.formats');
    var Model = require('web.Model');
    var Widget = require('web.Widget');
    var FormView = require('web.FormView');
    var ListView = require("web.ListView");
    var list_widget_registry = core.list_widget_registry;
    /*
    通过ListView进行扩展，让其能够根据tree设定的属性，用不同的样式来展示不同行
    用法：在tree上设置属性 decoration-tyibs-red ,系统实际会将其替换为 tyibs-row-red ,
         其他样式同上，可根据自己的需要定义不同的样式来展示。但条件一定是以 decoration-tyibs 开始。
    */
    ListView.include({
        init:function(){
            var self = this;
            this._super.apply(this, arguments);
            this.decoration_tyibs = null;
        },
        willStart:function(){
            //console.log("listview will start method callled");
            this.decoration_tyibs = _.pick(this.fields_view.arch.attrs, function(value, key) {
                return key.indexOf("decoration-tyibs-") >= 0;
            });
            this.decoration_tyibs = _.mapObject(this.decoration_tyibs, function(value) {
                return py.parse(py.tokenize(value));
            });
            return this._super();
        },
        compute_decoration_classnames: function(record){
            var classnames = this._super(record);
            var context = _.extend({}, record.attributes, {
                uid: session.uid,
                current_date: moment().format('YYYY-MM-DD'),
                // TODO: time, datetime, relativedelta
                //为维保公司提醒增加的选项
                //一周前
                before_one_week: moment().add(-1,'weeks').format('YYYY-MM-DD'),
                //一周后
                after_one_week: moment().add(1,'weeks').format('YYYY-MM-DD'),
                //半年前
                before_half_year: moment().add(-6,'months').format('YYYY-MM-DD'),
                //半年后
                after_half_year: moment().add(6,'months').format('YYYY-MM-DD'),
                //一个月前
                before_one_month: moment().add(-1,'months').format('YYYY-MM-DD'),
                //一个月后
                after_one_month: moment().add(1,'months').format('YYYY-MM-DD'),
            });
            //console.log(context.current_date, context.after_one_month);
            _.each(this.decoration_tyibs, function(expr, decoration) {
            if (py.PY_isTrue(py.evaluate(expr, context))) {
                    classnames += ' ' + decoration.replace('decoration-tyibs', 'tyibs-row');
                }
            });
            return classnames;
        }
    });

    /*自定义列表视图中数量字段控件，点击数字后可以转向对应的列表*/
    var Column = list_widget_registry.get('field');
    var TyibsActionFilterColumn = Column.extend({
        _format:function(row_data, options){
            var source_model = this.source_model;
            var filter_field = this.source_field;
            var title = this.source_title;
            if(row_data[this.id]!=undefined && (row_data.id!=undefined)){
                var value = row_data[this.id].value;
                if(value){
                    var record_id = row_data.id.value;
                    var domain = "[['"+filter_field+"'"+",'='," + record_id +"]]";
                    return _.template("<a data-domain='<%-domain%>' data-source-title='<%-title%>' data-source-model='<%-model%>' href='#' class='tyibs_action_filter_link'><%-text%></a>")({
                        text: value,
                        domain: domain,
                        model: source_model,
                        title: title,
                    });
                }
            }
            return this._super(row_data,options);
        },
    });

    /*自定义列表视图中数量字段控件，对自定义控件的事件进行绑定*/
    ListView.List.include({
        render:function(){
            var result = this._super(this, arguments);
            var self = this;
            //console.log(self.$current);
            self.$current.delegate('a.tyibs_action_filter_link',
                'click', function()
                {
                    var domain = $(this).data("domain");
                    domain = eval(domain);
                    var model = $(this).data("source-model");
                    var title = $(this).data("source-title");
                    self.view.do_action({
                        name: title,
                        type: 'ir.actions.act_window',
                        res_model: model,
                        domain: domain,
                        views: [[false, 'list'],[false, 'form']],
                        target: 'current',
                        context: {}
                    });
                    return false;
                });
            return result;
        }
    });

    //添加一个列表中使用的字段widget，与action相关
    list_widget_registry.add('field.tyibs_action_filter_link', TyibsActionFilterColumn);
});
